package MyThread_demo.duoxiancheng_anquan_threads;

/**
 * @author ChenYongHao
 * @date 2023/4/28 13:59
 * @desc 普通同步方法
 */
class SellTicketa implements Runnable {
    private int tickets = 100;

    @Override
    public void run() {
        while (true) {
            sellTicket();
        }
    }

    private synchronized void sellTicket() {
        if (tickets > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
            tickets--;
        }
    }
}

public class SellTicket_synchronized {
    public static void main(String[] args) {
        SellTicketa st = new SellTicketa();
        Thread t1 = new Thread(st, "窗口1");
        Thread t2 = new Thread(st, "窗口2");
        Thread t3 = new Thread(st, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}
